﻿
function getRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}
var x
function InsertImage(sender, args) {
    if (x != null) {
        // alert();

        var item = x.get_item();

        //If file (and not a folder) is selected - call the OnFileSelected method on the parent page
        if (item.get_type() == Telerik.Web.UI.FileExplorerItemType.File) {
            // Cancel the default dialog;
           x.set_cancel(true);

            // get reference to the RadWindow
            var wnd = getRadWindow();

            //Get a reference to the opener parent page using RadWndow
            var openerPage = wnd.BrowserWindow;

            //if you need the URL for the item, use get_url() instead of get_path()
            openerPage.OnFileSelected(wnd, item.get_path()); // Call the method declared on the parent page

            //Close the window which hosts this page
            wnd.close();
        }
    }
    //    var wnd = getRadWindow();
    //    wnd.close();
    //return confirm('OnClientClick script: Press OK to continue.');

}

function OnClientItemSelected(sender, args) {
    previewImage(args.get_path());

    x = args;

}

function OnClientDelete(explorer, args) {
    previewImage("");
}

function previewImage(src) {
    var isImageSrc = src.match(/\.(gif|jpg|png)$/gi);

    var pvwImage = $get("pvwImage");
    pvwImage.src = src;
    pvwImage.style.display = isImageSrc ? "" : "none";
    pvwImage.alt = src.substring(src.lastIndexOf('/') + 1);
}